package org.vectrics.recruiter.web.tags;

import org.vectrics.recruiter.user.RecruitUserProfile;
import org.apache.log4j.Category;
import java.net.URL;
import java.io.InputStream;
import javax.servlet.jsp.tagext.TagSupport;




public class NoPicsTag extends TagSupport {
    private static Category log = Category.getInstance(NoPicsTag.class);
    private String entryTypeParam = "";
    private RecruitUserProfile userProfile = null;

    public NoPicsTag() {
    }
    
/*
    protected String doRender(Request request, Properties params) {
        String path = request.getParameter("path");
        String content = getContent(path);
        return(content);
    };
  */      
        
    private String getContent(String path) {
        String content = "";
        try {
            StringBuffer str = new StringBuffer();
            URL url = new URL(path);
            InputStream input = url.openStream();
            try {
                int c;
                do {
                    c = input.read();
                    if (c != -1)
                        str.append((char)c);
                } while (c != -1);
                input.close();
                content = str.toString();
                int pos1 = content.indexOf("<body");
                int pos2 = content.indexOf("</body>");
                content = content.substring(pos1 + 5, pos2);
                int pos3 = content.indexOf(">");
                content = "<!-- BEGIN-NO-PIC-->" + content.substring(pos3 + 1)
                            + "<!-- END-NO-PIC -->";
                content = replaceImages(content);
            }
            catch (Exception ex) {
                str.append(ex.getMessage());
                log.error("Exception loading: " + path, ex);
            };
        }
        catch (Exception ex1) {
            log.error("Error getting content", ex1);
        
            //content = "<b>Error finding page: " + request.getParameter("path") + "</b>";
        }
                
        
        return(content);
    }
    
    
    
    private String replaceImages(String contentParam) {
        int i = 0;
        String newContent = contentParam;
        boolean done = false;
        while ((i < newContent.length()) && (done == false)) {
            int pos1 = newContent.indexOf("<img", i);
            if (pos1 < 0) 
                pos1 = newContent.indexOf("<IMG", i);
            if (pos1 > 0) {
                StringBuffer str = new StringBuffer();
                int pos2 = newContent.indexOf( ">", pos1 + 1);
                str.append(newContent.substring(0, pos1 - 1));
                str.append("<!-- REMOVED IMAGE -->");
                str.append(newContent.substring(pos2 + 1));
                i = pos2 + 1;
                newContent = str.toString();
            }
            else {
                done = true;
            }
            //i++;
        }
        return(newContent);
    }
    
    
}